package com.drools.excel;

import java.io.*;

import com.drools.entity.People;
import org.drools.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;

/**
 * Created by LANCHUNQIAN on 2016/11/21.
 */
public class ExcelCompiler {
    /**
     * 将xls决策表解析为普通规则文件
     * @throws IOException
     */
    public static File ruleTableConvertDRL(String ruleTablePath) throws IOException {
        SpreadsheetCompiler compiler = new SpreadsheetCompiler();
        File inFile = new File(ruleTablePath);
        File outFile = new File("E://outFile.drl");
        InputStream xlsStream = null;
        OutputStream fos = null;
        OutputStreamWriter osw = null;
        BufferedWriter out = null;
        try {
            xlsStream = new FileInputStream(inFile);
            String strRule = compiler.compile(xlsStream, InputType.XLS);
            fos = new FileOutputStream(outFile);
            osw = new OutputStreamWriter(fos);
            out = new BufferedWriter(osw);
            out.write(strRule);
            out.flush();
            System.out.println("\n" + strRule);
        } catch (IOException e) {
            throw new IOException("没找到文件或写入文件错误");
        }finally{
            xlsStream.close();
            fos.close();
            osw.close();
            out.close();
        }
        return outFile;
    }

    public static void main(String[] args) {
        String filePath = "E://drools.xls";
        try{
            File rulefile = ruleTableConvertDRL(filePath);
        }catch (IOException e){
            e.printStackTrace();
        }

        People people = new People("l",20,123,25);
        People people1 = new People("c",30,123,35);

    }

}
